﻿namespace TY.Client
{
    using DotNetty.Buffers;
    using DotNetty.Codecs;
    using DotNetty.Codecs.Protobuf;
    using DotNetty.Transport.Bootstrapping;
    using DotNetty.Transport.Channels;
    using DotNetty.Transport.Channels.Sockets;
    using Google.Protobuf;
    using MO.Protocol;
    using System;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading.Tasks;

    class Program
    {
        static async Task RunClientAsync()
        {


            var group = new MultithreadEventLoopGroup();
            var STRING_ENCODER = new ProtobufEncoder();
            var STRING_DECODER = new ProtobufDecoder(BasicCommand.Parser);

            string targetHost = null;

            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                    .Group(group)
                    .Channel<TcpSocketChannel>()
                    .Option(ChannelOption.TcpNodelay, true)
                    .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
                    {
                        IChannelPipeline pipeline = channel.Pipeline;


                        pipeline.AddLast(STRING_ENCODER);
                        pipeline.AddLast(STRING_DECODER);
                        // pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter()));
                        pipeline.AddLast(new ClientHandler());
                    }));

                IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"),9999));

                for (; ; )
                {
                    string line = Console.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    try
                    {


                        var message = new BasicCommand();
                        message.ActionId = 1111;
                        message.Req = new BasicReq();
                        message.Req.LoginReq=new LoginReq() { DeviceId = line, Localversion = line + DateTime.Now.ToString("yyyyMMdd") };
                        await  bootstrapChannel.WriteAndFlushAsync(message);


                            Console.WriteLine("消息发送完毕");
                       
                    }
                    catch
                    {
                    }
                    if (string.Equals(line, "bye", StringComparison.OrdinalIgnoreCase))
                    {
                        await bootstrapChannel.CloseAsync();
                        break;
                    }
                }
                Console.WriteLine("结束");
                Console.ReadLine();

                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait(1000);
            }
        }

        //static void Main() => RunClientAsync().Wait();

        static void Main() { 
        
        
        }
    }


  
}
